package com.cloud.service.impl;

import com.cloud.service.DemoTableStudentService;
import com.cloud.dao.DemoTableStudentDao;
import com.cloud.dto.DemoTableStudentDto;
import com.cloud.model.DemoTableStudent;
import com.cloud.enums.ResultEnum;
import com.cloud.exception.BusinessException;
import com.cloud.util.BeanUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * Created by util on 2018-4-27.
 */
@Service
public class DemoTableStudentServiceImpl implements DemoTableStudentService {

    @Autowired
    private DemoTableStudentDao demoTableStudentDao;

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean insertDemoTableStudent(DemoTableStudentDto demoTableStudentDto){
    	DemoTableStudent demoTableStudent = new DemoTableStudent();
    	BeanUtil.copyProperties(demoTableStudentDto, demoTableStudent);
        int num = demoTableStudentDao.insertDemoTableStudent(demoTableStudent);
        success(num);
        return true;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean deleteDemoTableStudent(DemoTableStudentDto demoTableStudentDto){
    	DemoTableStudent demoTableStudent = new DemoTableStudent();
    	BeanUtil.copyProperties(demoTableStudentDto, demoTableStudent);
        int num = demoTableStudentDao.deleteDemoTableStudent(demoTableStudent);
        success(num);
        return true;
    }

	private void success(int num) {
        if (num != 1) {
            throw new BusinessException(ResultEnum.fail);
        }
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public boolean updateDemoTableStudent(DemoTableStudentDto demoTableStudentDto){
    	DemoTableStudent demoTableStudent = new DemoTableStudent();
    	BeanUtil.copyProperties(demoTableStudentDto, demoTableStudent);
        int num = demoTableStudentDao.updateDemoTableStudent(demoTableStudent);
        success(num);
        return true;
    }
    
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public DemoTableStudent selectDemoTableStudentById(DemoTableStudentDto demoTableStudentDto){
        DemoTableStudent demoTableStudent = demoTableStudentDao.selectDemoTableStudentById(demoTableStudentDto.getId());
        if (demoTableStudent == null) {
        	return new DemoTableStudent();
        }
        return demoTableStudent;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public List<DemoTableStudent> selectDemoTableStudentList(DemoTableStudentDto demoTableStudentDto){
    	DemoTableStudent demoTableStudent = new DemoTableStudent();
    	BeanUtil.copyProperties(demoTableStudentDto, demoTableStudent);
        return demoTableStudentDao.selectDemoTableStudentList(demoTableStudent);
    }
    
    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public PageInfo selectDemoTableStudentPage(DemoTableStudentDto demoTableStudentDto){
    	DemoTableStudent demoTableStudent = new DemoTableStudent();
    	BeanUtil.copyProperties(demoTableStudentDto, demoTableStudent);
        return demoTableStudentDao.selectDemoTableStudentPage(demoTableStudent, demoTableStudentDto.getPageNum(), demoTableStudentDto.getPageSize());
    }

}